package com.iot08.admin.service;

import com.iot08.common.entity.admin.Agent;
import com.iot08.common.entity.admin.Channel;
import com.iot08.common.entity.admin.ImportRecord;
import com.iot08.common.entity.app.EntryRecord;
import com.iot08.common.service.base.BaseAdminService;
import com.iot08.common.service.base.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 旧版的厂商 生产批次记录
 *
 * @Author: Weichao
 * @Date: 2024/08/07/13:43
 * @Description:
 */
@Slf4j
@Service
public class ImportRecordService extends BaseService<ImportRecord> {
//    @Resource
//    private ChannelService channelService;

    @Resource
    private EntryRecordService entryRecordService;


    /***
     * @Description: 查找未被替换的 ImportRecord ,全部转换成 EntryRecord,存储起来.
     * @Author: Weichao
     * @Date: 2024/8/2
     */
    public List<EntryRecord> doChangeImportRecordList() {
        //找 changeCCTime 不存在
        Criteria criteria = Criteria.where("changeCCTime").exists(false);
        List<ImportRecord> importRecordList = find(new Query().addCriteria(criteria));
        List<EntryRecord> entryRecordList = new ArrayList<>();
        for (ImportRecord itemA : importRecordList) {
            if (entryRecordService.count(new Query(Criteria.where("_id").is(itemA.getId()))) == 0) {
               EntryRecord entryRecord = new EntryRecord();
               entryRecord.setId(itemA.getId());
               entryRecord.setAgent(itemA.getAgent());
               entryRecord.setAgentUsername(itemA.getAgent_username());
               entryRecord.setImportNum(itemA.getImport_num());
               entryRecord.setTelecom(0);
               entryRecord.setActiveNum(itemA.getActive_num());
               entryRecord.setBindNum(itemA.getBind_num());
               entryRecord.setActiveRadio(itemA.getActive_radio());
               entryRecord.setImportTime(itemA.getImportTime());
               entryRecord.setImportPersonn(itemA.getImport_personn());
               entryRecordService.save(entryRecord);
               log.info("查找未被替换的 ImportRecord ,全部转换成 EntryRecord,存储起来.记录ID :{} 厂商名:{} channelCode:{}",itemA.getId(), itemA.getAgent_username(), itemA.getAgent());
               // 旧记录 记录 兼容时间
               itemA.setChangeCCTime(new Date());
               save(itemA);
            }
        }
        return entryRecordList;
    }

}
